﻿using System.Data;
using Marbles.Core.Connectors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Marbles.Core.BBS;
using Marbles.Core.Authentication;

namespace Marbles.Web.BBS
{
    public partial class ThreadList : Marbles.Core.Content.UserControl
    {

        public Int32 PageSize { get { return cGrdThreads.PageSize; } set { cGrdThreads.PageSize = value; } }


        public string ForumName { get { object obj = ViewState["ForumName"]; return (obj == null) ? "" : obj.ToString(); } set { ViewState["ForumName"] = value; } }
        private Core.BBS.Forum forum = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            InitialiseForm();
            LoadData();            
        }

        private void InitialiseForm()
        {
            bool isEditor = this.Page.IsEditor || this.Page.IsAdministrator;
            forum = new Core.BBS.Forum(this.CurrentWeb.SiteId, ForumName);
            cLnkCategory.Text = forum.CategoryTitle;
            cLnkTitle.Text = forum.Title;
            cLnkTitle.NavigateUrl = "Forum.aspx?f=" + ForumName;
            cLnkCategory.NavigateUrl = "Default.aspx";
            cLblDescription.Text = forum.Description;
            cBtnPost.OnClientClick = "window.location.href='Post.aspx?f=" + forum.ForumName + "&t=0';return false;";

            if (AuthenticationProvider.CreateProviderInstance().CurrentToken == null)
            {
                cBtnPost.Visible = forum.IsPublic;
            }
            else
            {
                if (!isEditor)
                {
                    cBtnPost.Visible = !forum.IsReadonly;
                }
            }
        }

        private void LoadData()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("SELECT * FROM forumitems WHERE siteid = @siteid AND forumname = @forumname AND parentpostid = @postid AND approved = 1 ORDER BY sticky DESC, postdate DESC");


            cmd.AddParameter("@siteid", forum.Siteid);
            cmd.AddParameter("@forumname", forum.ForumName);
            cmd.AddParameter("@postid", 0);
            cGrdThreads.DataSource = dbc.FetchDataTable(cmd);
            cGrdThreads.DataBind();
        }

        protected void cGrdThreads_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow) { return; }
            Label cLblLastPost = e.Row.FindControl("cLblLastPost") as Label;
            DataRowView dr = e.Row.DataItem as DataRowView;
            DateTime value = Convert.ToDateTime(dr["lastpostdate"]);
            if (value.Year <= 1980) { value = Convert.ToDateTime(dr["postdate"]); }
            cLblLastPost.Text = (value.Year <= 1980) ? "" : value.ToString("ddd") + " " + value.ToShortDateString() + " " + value.ToShortTimeString();
            
        }

        protected void cGrdThreads_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            cGrdThreads.PageIndex = e.NewPageIndex;
            LoadData();
        }

    }
}